//
//  QuestionCommentCell.m
//  suRead
//
//  Created by nancy  on 16/9/19.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "QuestionCommentCell.h"
#import "M_QuestionComment.h"
#import "FMDBHelpers.h"
#import "UIImageView+WebCache.h"

@interface QuestionCommentCell()
@property (weak, nonatomic) IBOutlet UIImageView *zanIcon;
@property (weak, nonatomic) IBOutlet UIImageView *userPhoto;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *commentContent;
@property (weak, nonatomic) IBOutlet UILabel *creatTimeLable;
@property (weak, nonatomic) IBOutlet UILabel *labelPraiseCount;
@property (weak, nonatomic) IBOutlet UILabel *labelCommentCount;
@property (weak, nonatomic) IBOutlet UIImageView *icon_comment;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;

@property (strong,nonatomic) M_QuestionComment *comment;
@end

@implementation QuestionCommentCell

+ (CGFloat)heightWithContent:(M_QuestionComment *)cellData_
{
    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    //顶部高度 + 底部高度
    CGFloat height_ = 60.0f;
    
    //点赞和评论总数的宽度
    NSString *zancomentsStr=[cellData_.f_support stringByAppendingString:cellData_.f_reply];
    CGFloat zancomentsStrWidth = [zancomentsStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f]} context:nil].size.width;
    //右侧点赞评论总数和图标的宽度
    CGFloat otherWidth=15.0f+3.0f+2.0f+zancomentsStrWidth;
    //用户名称高度
    CGFloat usernameHeight = [cellData_.f_username boundingRectWithSize:CGSizeMake(screenWidth - 80.0f - 15.0f-40.0f-otherWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil].size.height;
    height_ += usernameHeight;
    
    //评论内容的高度
    NSRange range = [cellData_.f_content rangeOfString:@"</span>"];//判断字符串是否包含
    if (range.location !=NSNotFound)
    {
        NSString *jsString                 = [NSString stringWithFormat:@"<style type=\"text/css\"> \n"
                                              "span,pre{color: #9e9e9f;font-size: .8rem;}"
                                              "</style> \n"
                                              "%@ \n",cellData_.f_content];
        NSAttributedString * attrStr       = [[NSAttributedString alloc] initWithData:[jsString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        CGFloat contentHeight  = [attrStr boundingRectWithSize:CGSizeMake(screenWidth - 80.0f - 15.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size.height;
        
        
        height_ += contentHeight;
    }
    else {
        
        CGFloat contentHeight = [cellData_.f_content boundingRectWithSize:CGSizeMake(screenWidth - 80.0f - 15.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]} context:nil].size.height;
        height_ += contentHeight;
    }
    
    return height_;
}

- (void)setCellData:(M_QuestionComment *)cellData_
{
    self.comment = cellData_;
    
    self.userPhoto.layer.cornerRadius  = 20.0;//设置图片圆角的尺度
    self.userPhoto.layer.masksToBounds = YES;//没这句话它圆不起来
    //    self.userPhoto.image = [UIImage imageNamed:@"icon_user"];
    [self.userPhoto sd_setImageWithURL:[NSURL URLWithString:cellData_.f_img] placeholderImage:[UIImage imageNamed:@"icon_user"]];
    
    
    
    self.nameLable.text = cellData_.f_username;
    
   
    NSRange range = [cellData_.f_content rangeOfString:@"</span>"];//判断字符串是否包含
    if (range.location !=NSNotFound)
    {
        
        NSString *jsString                 = [NSString stringWithFormat:@"<style type=\"text/css\"> \n"
                                              "span,pre{color: #9e9e9f;font-size: .8rem;}"
                                              "</style> \n"
                                              "%@ \n",cellData_.f_content];
        NSAttributedString * attrStr       = [[NSAttributedString alloc] initWithData:[jsString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        self.commentContent.attributedText = attrStr;
        
    } else {
        
        self.commentContent.text = cellData_.f_content;
    }
    
    if([cellData_.f_reply isEqualToString:@"1"]){
        self.labelCommentCount.hidden = YES;
        self.icon_comment.hidden = YES;
        self.commentButton.hidden = YES;
    }else{
        self.labelCommentCount.hidden = NO;
        self.icon_comment.hidden = NO;
        self.commentButton.hidden = NO;
    }
    
    self.creatTimeLable.text = cellData_.f_inputtime;
    self.labelPraiseCount.text = cellData_.f_support;
    self.labelCommentCount.text = cellData_.f_commnums;
    
    if ([[FMDBHelpers sharedInstance] queryData:cellData_.f_id type:4]) {
        [self.zanIcon setImage:[UIImage imageNamed:@"btn_praised"]];
    } else {
        [self.zanIcon setImage:[UIImage imageNamed:@"icon_praise_count"]];
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (IBAction)zan:(id)sender
{
    if (![[FMDBHelpers sharedInstance] queryData:self.comment.f_id type:4]) {
        [[FMDBHelpers sharedInstance] insertData:self.comment.f_id andType:4];
        [self.zanIcon setImage:[UIImage imageNamed:@"btn_praised"]];
        int intString = [self.comment.f_support intValue]+1;
        NSString *newSquare=[NSString stringWithFormat:@"%d",intString];
        self.labelPraiseCount.text = newSquare;
        if (self.zanClickCallBack) {
            self.zanClickCallBack();
        }
    } else {
        //不能重复点赞
    }
}

- (IBAction)reply:(id)sender
{
    if (self.replyClickCallBack) {
        self.replyClickCallBack();
    }
}

@end
